<template>
    <el-dialog :title="dialogStatus | titleFilter" :close-on-click-modal="false" width="600px" :visible.sync="visible">
        <el-form ref="temp" :model="temp" :rules="rule" label-width="80px" @keyup.enter.native="dataFormSubmit()">

            <el-form-item label="字典名称" prop="dictName">
                <el-input v-model="temp.dictName" placeholder="字典名称" />
            </el-form-item>

            <el-form-item label="字典类型" prop="dictType">
                <el-input v-model="temp.dictType" placeholder="字典类型" />
            </el-form-item>

            <el-form-item label="字典状态" prop="status">
                <el-radio-group v-model="temp.status">
                    <el-radio :label="0">禁用</el-radio>
                    <el-radio :label="1">启用</el-radio>
                </el-radio-group>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">
                取消
            </el-button>
            <el-button type="primary" @click="dataFormSubmit">
                确认
            </el-button>
        </span>
    </el-dialog>
</template>
<script>
import { getDictionaryInfo, createDictionaryList, updateDictionaryList } from '@/api/permission'
export default {
    filters: {
        // 过滤弹框的标题
        titleFilter (type) {
            const textMap = {
                create: '新增',
                update: '修改'
            }
            return textMap[type]
        }
    },
    data () {
        return {
            visible: false,
            dialogStatus: '', // 弹框新增或者修改
            temp: { // 表单数据
                dictId: '',
                dictName: '',
                dictType: '',
                status: 1
            },
            rule: {
                roleName: [
                    { required: true, message: '角色名称不能为空', trigger: 'blur' }
                ],
                dictType: [
                    { required: true, message: '字典类型不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    created () {
    },
    methods: {
        // 初始化
        init (status, param) {
            this.dialogStatus = status // 标题
            this.visible = true // 显示
            this[status](param) // 新增或者修改
            this.$nextTick(() => { // 重置表单
                this.$refs['temp'].clearValidate()
            })
        },
        create () {
            this.resetTemp() // 清空表单
        },
        async update (param) {
            // let data = Object.assign({}, param)  .//从上一页带过来当前的数据
            const { data, code } = await getDictionaryInfo(param.dictId) // 从后台获取当前的的数据
            // 成功
            this.temp.dictId = data.dictId
            this.temp.dictName = data.dictName
            this.temp.dictType = data.dictType
            this.temp.status = data.status
        },
        // 新增数据清空表单数据
        resetTemp () {
            this.temp = {
                dictId: '',
                dictName: '',
                dictType: '',
                status: 1
            }
        },
        // 表单提交
        dataFormSubmit () {
            this.$refs['temp'].validate(async (valid) => {
                if (!valid) return
                const params = {
                    'dictId': this.temp.dictId || undefined,
                    'dictName': this.temp.dictName,
                    'dictType': this.temp.dictType,
                    'status': this.temp.status
                }
                const textMap = {
                    create: createDictionaryList, // 新增
                    update: updateDictionaryList // 修改
                }

                // 提交表单
                const { code, message } = await textMap[this.dialogStatus](params)

                // 成功
                this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.visible = false
                        this.$emit('getList')
                    }
                })
            })
        }

    }
}
</script>

<style lang="scss">
.mod-menu {
    .menu-list__input,
    .icon-list__input {
        > .el-input__inner {
            cursor: pointer;
        }
    }
    &__icon-popover {
        width: 458px;
        overflow: hidden;
    }
    &__icon-inner {
        width: 478px;
        max-height: 258px;
        overflow-x: hidden;
        overflow-y: auto;
    }
    &__icon-list {
        width: 458px;
        padding: 0;
        margin: -8px 0 0 -8px;
        > .el-button {
            padding: 8px;
            margin: 8px 0 0 8px;
            > span {
                display: inline-block;
                vertical-align: middle;
                width: 18px;
                height: 18px;
                font-size: 18px;
            }
        }
    }
    .icon-list__tips {
        font-size: 18px;
        text-align: center;
        color: #e6a23c;
        cursor: pointer;
    }
}
</style>
